package com.imooc.mall.model.controller;

import com.github.pagehelper.PageInfo;
import com.imooc.mall.model.common.ApiRestResponse;
import com.imooc.mall.model.common.Constant;
import com.imooc.mall.model.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.model.request.AddCategoryReq;
import com.imooc.mall.model.request.UpdateCategoryReq;
import com.imooc.mall.model.service.CategoryService;
import com.imooc.mall.model.service.UserService;
import com.imooc.mall.model.vo.CategoryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
public class CategoryController {

    @Autowired
    UserService userService;

    @Autowired
    CategoryService categoryService;

    @ApiOperation("后台添加目录")
    @PostMapping("/admin/category/add")
    @ResponseBody
    public ApiRestResponse addCategory(HttpSession session,@Valid @RequestBody AddCategoryReq addCategoryReq){
//        @RequestBody 是spring可以在body里获得参数
//            查看是否有登陆，在下面用过滤器实现
        User currentUser =(User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if(currentUser==null){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOAD);
        }
        //校验是否是管理员
        boolean adminRole=userService.checkAdminRole(currentUser);
        if(adminRole){
//            是管理员，添加
            categoryService.add(addCategoryReq);
            return ApiRestResponse.success();
        }else {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
    }

    @ApiOperation("后台更新目录")
    @PostMapping("/admin/category/update")
    @ResponseBody
    public ApiRestResponse updateCategory(HttpSession session, @Valid @RequestBody UpdateCategoryReq updateCategoryReq){
//        @RequestBody 是spring可以在body里获得参数

        User currentUser =(User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if(currentUser==null){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOAD);
        }
        //校验是否是管理员
        boolean adminRole=userService.checkAdminRole(currentUser);
        if(adminRole){
//            是管理员，更新
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryReq,category);
            categoryService.update(category);
            return ApiRestResponse.success();
        }else {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
    }


    @ApiOperation("后台删除目录")
    @PostMapping("/admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(@RequestParam Integer id){
        categoryService.delete(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台查看目录")
    @PostMapping("/admin/category/list")
    @ResponseBody
    public ApiRestResponse listForAdmin(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageInfo pageInfo = categoryService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("前端查看目录")
    @PostMapping("/category/list")
    @ResponseBody
    public ApiRestResponse listForCustomer(){
        List<CategoryVo> categoryVoList = categoryService.listForCustomer(0);
        return ApiRestResponse.success(categoryVoList);
    }

}
